package com.halfsword.creativecraft.gui.machine;

import com.halfsword.creativecraft.CreativeCraft;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 空间碎片收集器GUI界面
 */
public class SpaceCollectorScreened extends AbstractContainerScreen<SpaceCollectorMenu> {

    // ================ 纹理资源 ================
    public static final ResourceLocation TEXTURE =
            ResourceLocation.fromNamespaceAndPath(CreativeCraft.MOD_ID, "textures/gui/space_collector.png");

    // ================ 进度条常量 ================
    private static final int PROGRESS_WIDTH = 73;  // 进度条宽度
    private static final int PROGRESS_HEIGHT = 53; // 进度条高度
    private static final int PROGRESS_X = 33;      // 进度条左上角X坐标（相对GUI）
    private static final int PROGRESS_Y = 17;      // 进度条左上角Y坐标（相对GUI）
    private static final int PROGRESS_U = 178;     // 进度条纹理起始U坐标
    private static final int PROGRESS_V = 6;       // 进度条纹理起始V坐标

    private static final DecimalFormat TIME_FORMAT = new DecimalFormat("0.0");

    // ================ 构造函数 ================
    public SpaceCollectorScreened(SpaceCollectorMenu menu, Inventory playerInventory, Component title) {
        super(menu, playerInventory, title);
        this.imageWidth = 176; // GUI 宽度
        this.imageHeight = 166; // GUI 高度
    }

    // ================ 界面初始化 ================
    @Override
    protected void init() {
        super.init();
        this.titleLabelX = (this.imageWidth - this.font.width(this.title)) / 2;
        this.titleLabelY = 10000;
        this.inventoryLabelY = 10000;
    }

    // ================ 背景渲染 ================
    @Override
    protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        RenderSystem.setShaderTexture(0, TEXTURE);

        int x = (width - imageWidth) / 2;
        int y = (height - imageHeight) / 2;

        // 绘制背景
        guiGraphics.blit(TEXTURE, x, y, 0, 0, imageWidth, imageHeight);

        // 仅在机器工作时渲染进度条
        if (menu.isCrafting()) {
            renderProgressBar(guiGraphics, x, y);
        }
    }

    // ================ 进度条渲染 - 修改为水平方向 ================
    private void renderProgressBar(GuiGraphics guiGraphics, int guiLeft, int guiTop) {
        // 计算当前进度宽度（像素）
        int progressWidth = (int) (menu.getProgressPercent() * PROGRESS_WIDTH);

        // 绘制水平进度条（从左向右填充）
        guiGraphics.blit(
                TEXTURE,
                guiLeft + PROGRESS_X,       // X起始位置不变
                guiTop + PROGRESS_Y,        // Y位置不变
                PROGRESS_U,                 // 纹理起始U坐标
                PROGRESS_V,                 // 纹理起始V坐标
                progressWidth,              // 绘制宽度根据进度变化
                PROGRESS_HEIGHT             // 高度保持完整
        );
    }

    // 检查鼠标是否在槽位上
    private boolean isMouseOverSlot(double mouseX, double mouseY) {
        for (Slot slot : this.menu.slots) {
            if (isHovering(slot.x, slot.y, 16, 16, mouseX, mouseY)) {
                return true;
            }
        }
        return false;
    }

    // ================ 整体渲染 ================
    @Override
    public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        renderBackground(guiGraphics);
        super.render(guiGraphics, mouseX, mouseY, partialTick);
        renderTooltip(guiGraphics, mouseX, mouseY);

        // 修改后的进度提示逻辑：仅当不在槽位上时显示
        if (isHovering(PROGRESS_X, PROGRESS_Y, PROGRESS_WIDTH, PROGRESS_HEIGHT, mouseX, mouseY)
                && !isMouseOverSlot(mouseX, mouseY)) {

            // 计算当前进度百分比
            int progressPercent = (int)(menu.getProgressPercent() * 100);

            // 计算剩余时间（秒）
            int currentProgress = menu.getProgress();
            int maxProgress = menu.getMaxProgress();
            float remainingTicks = maxProgress - currentProgress;
            float remainingSeconds = remainingTicks / 20f; // 20 ticks = 1秒
            if (remainingSeconds==10.0) {remainingSeconds=0;}

            // 创建多行提示文本
            List<Component> tooltipLines = List.of(
                    Component.translatable("gui.creativecraft.progress_percent", progressPercent),
                    Component.translatable("gui.creativecraft.remaining_time", TIME_FORMAT.format(remainingSeconds))
            );

            // 正确渲染多行提示 (修复方法签名)
            guiGraphics.renderComponentTooltip(this.font, tooltipLines, mouseX, mouseY);
        }
    }
}